package streaming.test.rlm.call.video;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Vector;

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.ConfigureCompleteEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSinkException;
import javax.media.NoProcessorException;
import javax.media.Processor;
import javax.media.RealizeCompleteEvent;
import javax.media.control.FormatControl;
import javax.media.control.TrackControl;
import javax.media.datasink.DataSinkEvent;
import javax.media.datasink.DataSinkListener;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.protocol.ContentDescriptor;
import javax.media.protocol.DataSource;
import javax.media.rtp.SessionAddress;


public class CallingPeer implements ControllerListener, DataSinkListener{


	Processor processor;
	AudioFormat RTPformatA = new AudioFormat(AudioFormat.GSM_RTP);
	VideoFormat RTPformat = new VideoFormat(VideoFormat.JPEG_RTP);
	AudioFormat formatA = new AudioFormat(AudioFormat.LINEAR, 8000, 8, 1);
	VideoFormat format = new VideoFormat(VideoFormat.RGB);
	public CallingPeer(){

		//capturing

		// audio
		//		JavaSound audio capture : javasound://44100
		//		DirectSoundCapture : dsound://
		
//		AudioFormat format = new AudioFormat(AudioFormat.GSM);

		Vector<CaptureDeviceInfo> devices = (Vector<CaptureDeviceInfo>)CaptureDeviceManager.getDeviceList(format);

		CaptureDeviceInfo videoDevice = devices.get(0);
//		for(CaptureDeviceInfo dev : devices){
//			System.out.println(dev.getName());
//			System.out.println(dev);
//		}
		
		
//		videoDevice = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
//		CaptureDeviceInfo soundDevice = CaptureDeviceManager.getDevice("DirectSoundCapture");
		
//		
//		MediaLocator locator = soundDevice.getLocator();	
		MediaLocator locator = videoDevice.getLocator();	


		try {
			processor = Manager.createProcessor(locator);
			processor.addControllerListener(this);
			processor.configure();
			








		} catch (NoProcessorException e) {
			System.out.println("Unable to find specified processor");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Something in the IO");
			e.printStackTrace();
		}


	}

	public static void main(String[] args) {
		new CallingPeer();
	}

	@Override
	public void controllerUpdate(ControllerEvent e) {
		// TODO Auto-generated method stub
		System.out.println(e);
		
		if (e instanceof ConfigureCompleteEvent){
			if(processor != null && e.getSource().equals(processor)){

				processor.setContentDescriptor(new ContentDescriptor(ContentDescriptor.RAW_RTP));
				
				TrackControl[] trackControls = processor.getTrackControls();

				boolean encodingOK = false;

				for(TrackControl track : trackControls){
					if( !encodingOK && track instanceof FormatControl){
						if(((FormatControl)track).setFormat(RTPformat) == null){
							track.setEnabled(false);
						}
						else{
							encodingOK = true;
						}
					}
				}


				if(encodingOK)
					processor.realize();
			}


		}

		if (e instanceof RealizeCompleteEvent){

			DataSource ds = null;

			ds = processor.getDataOutput();

			String url = "rtp://192.168.1.255:22222/video";
//			SessionAddress addr = null;
//			try {
//				addr = new SessionAddress(InetAddress.getByName("79.101.245.250"), 49150);
//			} catch (UnknownHostException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
			
			MediaLocator dest = new MediaLocator(url);

			DataSink sink = null;

			try {
				sink = Manager.createDataSink(ds, dest);
				sink.addDataSinkListener(this);
				processor.start();
				sink.open();
				sink.start();
			} catch (NoDataSinkException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} catch (SecurityException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			} catch (IOException ex) {
				// TODO Auto-generated catch block
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void dataSinkUpdate(DataSinkEvent e) {
		
		System.out.println(e);
	}



}
